<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片布局 -->
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <!-- 使用 Tabs 组件完成标签页布局 -->
          <el-tabs v-model="activeName">
            <el-tab-pane label="组织架构" name="first" class="tab-pane">
              <!-- 头部区域 -->
              <el-row type="flex">
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">上善若水,大智若愚</span>
                </el-col>
                <el-col :span="4">
                  <el-row type="flex">
                    <el-col>负责人</el-col>
                    <el-col>操作
                      <el-dropdown>
                        <span class="el-dropdown-link">
                          <i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item>添加子部门</el-dropdown-item>
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <!-- 使用树形结构 -->
              <el-tree
                class="departments-tree"
                :data="treeData"
                :props="defaultProps"
                :default-expand-all="true"
              >
                <template v-slot="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    style="height: 50px; width: 100%"
                    align="middle"
                  >
                    <el-col :span="20">{{ data.name }}</el-col>
                    <el-col :span="4">
                      <el-row type="flex">
                        <el-col>{{ data.manager }}</el-col>
                        <el-col>
                          <el-dropdown>
                            <span class="el-dropdown-link">
                              操作<i
                                class="el-icon-arrow-down el-icon--right"
                              />
                            </span>
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item
                                @click.native="addDept(data.id)"
                              >添加子部门</el-dropdown-item>
                              <el-dropdown-item
                                @click.native="editDept(data.id)"
                              >编辑部门</el-dropdown-item>
                              <!-- 如果当前部门有子部门,就不应该显示删除 -->
                              <!-- v-if="data && !data.children" -->
                              <!-- v-if="data.children.length === 0" -->
                              <el-dropdown-item
                                v-if="data && !data.children"
                                @click.native="delDept(data.id)"
                              >删除部门</el-dropdown-item>
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>
            </el-tab-pane>
          </el-tabs>
          <!-- 弹窗 -->
          <el-dialog

            :title="isEdit ? '编辑角色' : '新增角色'"
            :visible.sync="showDialog"
            width="80%"
            :close-on-press-escape="false"
            :close-on-click-modal="false"
          >
            <!-- :title=" isEdit ? '编辑部门' :'添加部门' -->
            <dept-dialog
              v-if="showDialog"
              :parent-id="parentId"
              :update-depart="updateDepart"
              :is-edit="isEdit"
              :origin-list="originList"
              @closeDialog="closeDialog"
              @shiftDialog="shiftDialog"
            />
          </el-dialog>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import DeptDialog from './deptDialog'
import { getDepartments, delDepartment } from '@/api/user'
export default {
  name: 'Departments',
  components: {
    DeptDialog
  },
  data() {
    return {
      originList: [], // 接收父亲传过来的精简数据
      activeName: 'first',
      treeData: [], // 接收组织架构
      showDialog: false, // 弹框展示默认关闭
      parentId: '', // 父级id
      isEdit: false, // 编辑状态
      // 定义结构显示
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartmentsFn() // 企业的部门列表调用
  },
  methods: {
    // 删除部门
    async delDept(id) {
      const res1 = await this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })

        .catch((err) => err)
      // 用户点击取消
      if (res1 === 'cancel') return this.$message('已取消删除')
      const res = await delDepartment(id)
      console.log(res)
      this.$message.success(res.message)// 删除成功
      this.getDepartmentsFn() // 企业的部门列表调用
    },

    // 用户点击了取消
    shiftDialog() {
      this.showDialog = false
    },
    // 编辑部门
    editDept(id) {
      this.parentId = id
      this.showDialog = true // 点击弹框展开
      this.isEdit = true // 是编辑状态
    },
    // 更新部门
    updateDepart() {
      this.showDialog = false // 点击弹框关闭
      this.getDepartmentsFn() // 企业的部门列表调用
    },
    // 添加部门
    addDept(id) {
      console.log(id)
      this.parentId = id
      this.showDialog = true // 点击弹框展开
      this.isEdit = false // 不是编辑状态
    },
    // 查询企业的部门列表
    async getDepartmentsFn() {
      const res = await getDepartments()
      // console.log(res)
      // 父亲传给儿子的精简数据
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        pid: item.pid,
        code: item.code
      }))
      this.treeData = tranListToTreeData(res.data.depts)
    },
    // 关闭弹窗,刷新数据
    closeDialog() {
      this.showDialog = false
      this.getDepartmentsFn() // 企业的部门列表调用
    }
  }
}
</script>
<style lang="scss" scoped>
.departments-tree {
  margin: 12px 0px;
}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 60px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
